package cn.tedu.hm.controller;

import cn.tedu.hm.pojo.dto.RoomAddNewDTO;
import cn.tedu.hm.pojo.dto.RoomUpdateDTO;
import cn.tedu.hm.pojo.vo.RoomListItemVO;
import cn.tedu.hm.pojo.vo.RoomStandardVO;
import cn.tedu.hm.service.IRoomService;
import cn.tedu.hm.wep.JsonPage;
import cn.tedu.hm.wep.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;

import java.util.UUID;


/**
 * 说明：
 *
 * @author:ZhouYong
 * @Date: 2023/3/11
 **/
@Api(tags = "04. 房间管理模块")
@Slf4j
@RestController
@RequestMapping("/rooms")
@Validated
@Transactional
public class RoomController {

    @Autowired
    private IRoomService roomService;

    public RoomController() {
        log.debug("创建控制器类对象 RoomController");
    }

    @PostMapping("/add-new")
    @ApiOperation("添加房间")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(RoomAddNewDTO roomAddNewDTO) {
        log.debug("开始处理【添加房间】的请求，参数：{}", roomAddNewDTO);
        roomService.addNew(roomAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/upload")
    @ApiOperation("添加房间图片")
    @ApiOperationSupport(order = 110)
    public String upload(MultipartFile picFile) throws IOException {
        log.debug("开始处理【添加房间图片】的请求，参数：{}", picFile);
        String fileName = picFile.getOriginalFilename();
        System.out.println("文件名" + fileName);
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;
        String dirPath = "C:\\Users\\Administrator\\IdeaProjects\\web_test\\public\\";
        File dirFile = new File(dirPath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        String filePath = dirPath + "/" + fileName;
        picFile.transferTo(new java.io.File(filePath));
        System.out.println("保存完成");
        return fileName;
    }
    @PostMapping("/remove")
    public void remove(String fileName) {
        new File("C:\\Users\\Administrator\\IdeaProjects\\web_test\\public\\"+fileName).delete();
    }


    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除房间")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "房间ID", required = true, dataType = "long")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID删除房间】的请求，参数：{}", id);
        roomService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用房间")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房间ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【启用房间】的请求，参数：{}", id);
        roomService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用房间")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房间ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【禁用房间】的请求，参数：{}", id);
        roomService.setDisable(id);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改房间详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "房间ID", required = true, dataType = "long")
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id,
                                           @Valid RoomUpdateDTO roomUpdateDTO) {
        log.debug("开始处理【修改房间详情】的请求，ID：{}，新数据：{}", id, roomUpdateDTO);
        roomService.updateInfoById(id, roomUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/selectByRoomName")
    @ApiOperation("根据RoomName查询房间详情")
    @ApiOperationSupport(order = 430)
    public JsonResult<JsonPage<RoomStandardVO>> selectByRoomName(String roomName, Integer page, Integer pageSize) {
        log.debug("开始处理【根据房间名称查询房间详情】的请求，参数：{}", roomName);
        JsonPage<RoomStandardVO> queryResult = roomService.getStandardByRoomName(roomName, page, pageSize);
        log.debug("data{}", queryResult);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/selectByRoomType")
    @ApiOperation("根据Type查询房间详情")
    @ApiOperationSupport(order = 431)
    public JsonResult<JsonPage<RoomStandardVO>> selectByRoomType(String type, Integer page, Integer pageSize) {
        log.debug("开始处理【根据房间类型查询房间详情】的请求，参数：{}", type);
        JsonPage<RoomStandardVO> queryResult = roomService.getStandardByRoomType(type, page, pageSize);
        log.debug("data{}", queryResult);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/selectByRoomNameAndRoomType")
    @ApiOperation("根据房间名称room Name和房间类型Type查询房间详情")
    @ApiOperationSupport(order = 432)
    public JsonResult<JsonPage<RoomStandardVO>> selectByRoomNameAndRoomType(String roomName, String type, Integer page, Integer pageSize) {
        log.debug("开始处理【根据房间类型查询房间详情】的请求，参数：{},{}", roomName, type);
        JsonPage<RoomStandardVO> queryResult = roomService.getStandardByRoomNameAndRoomType(roomName, type, page, pageSize);
        log.debug("data{}", queryResult);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询房间详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<RoomStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询房间详情】的请求，参数：{}", id);
        RoomStandardVO queryResult = roomService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @ApiOperation("查询房间列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<JsonPage<RoomListItemVO>> list(Integer page, Integer pageSize) {
        log.debug("开始处理【查询房间列表】的请求{},{}", page, pageSize);
        JsonPage<RoomListItemVO> list = roomService.list(page, pageSize);
        return JsonResult.ok(list);
    }


}
